package com.mbt.remote.server.event;

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;

import com.mbt.bluetooth.manager.BluetoothConnectionManager;
import com.mbt.remote.server.RemoteServer;
import com.mbt.remote.util.Log;

/**
 * The Class MobileEventsServer.
 */
public class MobileEventsServer implements Runnable {

	private static final String MOUSE_RIGHT_CLICK = "RIGHTCLICK";

	private static final String RELOADIMG = "RELOADIMG";
	
	private static final String KEYDOWN = "KEYDOWN";
	private static final String KEYUP = "KEYUP";
	private static final String KEYLEFT = "KEYLEFT";
	private static final String KEYRIGHT = "KEYRIGHT";
	private static final String TAB = "TAB";
	private static final String STARTMENU = "STARTMENU";
	private static final String ENTER = "ENTER";
	private static final String CLOSE = "CLOSE";
	/** The robot. */

	/** The size. */
	Rectangle size = null;

	/** The i robot. */
	Robot iRobot = null;

	/** The MOUSE. */
	private final String MOUSE = "MOUSE";

	/** The MOUS e_ click. */
	private final String MOUSE_CLICK = "MOUSECLICK";

	/** The KEYPRESS. */
	private final String KEYPRESS = "KEYPRESS";


	/**
	 * Instantiates a new mobile events server.
	 * 
	 * @throws AWTException
	 *             the aWT exception
	 */
	public MobileEventsServer() throws AWTException {
		this.iRobot = new Robot();
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		size = new Rectangle(0, 0, screenSize.width, screenSize.height);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Runnable#run()
	 */
	public void run() {
		Log.log("MobileEventsServer : Waiting to get client connection...");
		while (!BluetoothConnectionManager.isConnected())
			;
		UserInterface userInterface = new UserInterface(iRobot);

		while (RemoteServer.isRunning()) {
			Log.log("MobileEventsServer : client connection established...");
			if (!RemoteServer.isImageSentLock()) {
				String msg = BluetoothConnectionManager.accept()
						.recieveMessages();
				System.out.println("RCVD :" + msg);
				String msgArray[] = new String[3];
				if (msg != null && !"".equals(msg.trim())) {
					msgArray = msg.split("##");
					if (msgArray[0].equals(MOUSE)) {
						iRobot.mouseMove(Integer.parseInt(msgArray[1]), Integer
								.parseInt(msgArray[2]));
					} else if (msgArray[0].equals(MOUSE_CLICK)) {
						iRobot.mousePress(1);
						iRobot.mouseRelease(1);
					} else if (msgArray[0].equals(MOUSE_RIGHT_CLICK)) {
						iRobot.mousePress(3);
						iRobot.mouseRelease(3);
					} else if (msgArray[0].equals(KEYPRESS)) {
							userInterface.keyPressed(Integer
									.parseInt(msgArray[1]));
					} else if (msgArray[0].equals(RELOADIMG)) {
						RemoteServer.setReloadImg(true);
					}else if (msgArray[0].equals(ENTER)) {
						iRobot.keyPress(KeyEvent.VK_ENTER);
						iRobot.keyRelease(KeyEvent.VK_ENTER);
					}else if (msgArray[0].equals(CLOSE)) {
						iRobot.keyPress(KeyEvent.VK_ALT);
						iRobot.keyPress(KeyEvent.VK_F4);
						iRobot.keyRelease(KeyEvent.VK_F4);
						iRobot.keyRelease(KeyEvent.VK_ALT);
					}else if (msgArray[0].equals(STARTMENU)) {
						iRobot.keyPress(KeyEvent.VK_CONTROL);
						iRobot.keyPress(KeyEvent.VK_ESCAPE);
						iRobot.keyRelease(KeyEvent.VK_ESCAPE);
						iRobot.keyRelease(KeyEvent.VK_CONTROL);
					}else if (msgArray[0].equals(KEYDOWN)) {
						iRobot.keyPress(KeyEvent.VK_DOWN);
						iRobot.keyRelease(KeyEvent.VK_DOWN);
					}else if (msgArray[0].equals(KEYLEFT)) {
						iRobot.keyPress(KeyEvent.VK_LEFT);
						iRobot.keyRelease(KeyEvent.VK_LEFT);
					}else if (msgArray[0].equals(KEYRIGHT)) {
						iRobot.keyPress(KeyEvent.VK_RIGHT);
						iRobot.keyRelease(KeyEvent.VK_RIGHT);
					}else if (msgArray[0].equals(KEYUP)) {
						iRobot.keyPress(KeyEvent.VK_UP);
						iRobot.keyRelease(KeyEvent.VK_UP);
					}else if (msgArray[0].equals(TAB)) {
						iRobot.keyPress(KeyEvent.VK_TAB);
						iRobot.keyRelease(KeyEvent.VK_TAB);
					}
				}
			}
		}
	}

}
